const CompressionWebpackPlugin = require('compression-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin')
const webpack = require('webpack')
const path = require("path");
// const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i;
const Timestamp = new Date().getTime();
let quarkgisSource = './node_modules/quarkgis/Source'
let quarkgisWorkers = '../Build/QuarkGIS/Workers'
module.exports = {
    lintOnSave: false,// 关闭eslint 检查
    publicPath: './',
    assetsDir: 'static',
    outputDir: process.env.outputDir,
    productionSourceMap: false,
    devServer: {
        open: true,
        host: '0.0.0.0',
        port: 8080,
        https: false,
        hotOnly: false,
        sockHost: 'demo.my-domain.com',
        proxy: { // 设置代理
            '/api': {
                ws: false, // proxy websockets
                target: 'http://hubeishuiwu.hbzlwl.cn', // 测试服务器域名
                changeOrigin: true, //是否跨域
                pathRewrite: {
                    '^/api': 'api',
                }
            }
        },
        disableHostCheck: true
    },
    configureWebpack: config => {
        // 开发环境不需要gzip
        /*if (process.env.NODE_ENV !== 'production'){
            config.plugins.push(
                new CompressionWebpackPlugin({
                    algorithm: 'gzip',
                    test: productionGzipExtensions,
                    threshold: 10240,
                    minRatio: 0.8,
                    deleteOriginalAssets: false
                })
            );
        }*/
        // 开发和生产都需要配置quarkgis

        config.plugins.push(
            new CopyWebpackPlugin([{from: path.join(quarkgisSource, quarkgisWorkers), to: 'Workers'}]),
            new CopyWebpackPlugin([{from: path.join(quarkgisSource, 'Assets'), to: 'Assets'}]),
            new CopyWebpackPlugin([{from: path.join(quarkgisSource, 'Widgets'), to: 'Widgets'}]),
            new CopyWebpackPlugin([{from: path.join(quarkgisSource, 'ThirdParty/Workers'), to: 'ThirdParty/Workers'}]),
            new webpack.DefinePlugin({
                // Define relative base path in quarkgis for loading assets
                QUARKGIS_BASE_URL: JSON.stringify('./')
            }),
        );

    },
    // chainWebpack: config => {
    //     if (process.env.NODE_ENV === 'production') {
    //         config.output.filename('js/[name].' + Timestamp + '.js').end();
    //         config.output.chunkFilename('js/[name].' + Timestamp + '.js').end();
    //         config.plugin('extract-css').tap(args => [{
    //             filename: `css/[name].${Timestamp}.css`,
    //             chunkFilename: `css/[name].${Timestamp}.css`
    //         }])
    //     }
    // },
    css: {
        loaderOptions: {
            scss: {
                prependData: `@import "~@/assets/scss/_variables.scss";`
            },
        }
    }
};
